package cn.com.bluemoon.daps.standard.common.report;

import cn.com.bluemoon.daps.model.dto.ModelDto;
import cn.com.bluemoon.daps.model.dto.ModelTableDto;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * R 为结果
 * @author Jarod.Kong
 * @date 2020/9/27 10:25
 */
public abstract class AbstractReportCheck<R> implements IReportCheck {

    @Override
    public boolean generate(ModelDto modelDto) {
        List<ModelTableDto> tableModelDtos = modelDto.getTableModelDtos();
        List<R> results = tableModelDtos.parallelStream()
                .map(reportCheckFunc(modelDto))
                .collect(Collectors.toList());
        return checkPass(results);
    }

    /**
     * @return 报告实现函数
     */
    public abstract Function<ModelTableDto, R> reportCheckFunc(ModelDto modelDto);

    /**
     * 基于结果定义是否未通过
     *
     * @param list
     * @return
     */
    public abstract boolean checkPass(List<R> list);

    /**
     *
     * @param f1 1
     * @param f2 2
     * @return 处理两字段存在""或者null的对比，忽略
     */
    public boolean isEqualsByTrimAndToEmpty(String f1, String f2){
        return StringUtils.equals(StringUtils.trimToEmpty(f1), StringUtils.trimToEmpty(f2));
    }
}
